package com.jxzyk.model.center;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.entity.I_List;
import com.core.ui.activity.BaseActivity;
import com.core.util.T;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.jxzyk.R;
import com.jxzyk.adapter.CourseAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.Course;
import com.jxzyk.http.Query;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.FavouriteCoursesRequest;
import com.jxzyk.model.course.CourseActivity;
import com.jxzyk.util.UserManger;
import com.jxzyk.view.TitleLayout;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

@ContentView(R.layout.fra_course)
public class LeaningRecordActivity extends BaseActivity {
	@ViewInject(R.id.title)
	TitleLayout title;
	@ViewInject(R.id.list_course)
	PullToRefreshListView mPullRefreshListView;

	private ListView listView;

	@Override
	public void initData(Bundle bundle) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void initUi() {
		// TODO Auto-generated method stub
		title.setTtile("学习记录");
		title.setOnLeftListener(R.drawable.ic_chevron_left_white_48dp, new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				String label = DateUtils.formatDateTime(LeaningRecordActivity.this, System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
				// Update the LastUpdatedLabel
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				loadList(0);
			}
		});
		listView = mPullRefreshListView.getRefreshableView();
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Bundle bundle = new Bundle();
				try {
					Course course = (Course) adapter.getLists().get(arg2 - 1);
					bundle.putSerializable(course.getClass().getName(), course);
					showActivity(CourseActivity.class, bundle);
				} catch (Exception e) {
					e.printStackTrace();
					T.toast("下标越界[listsize]" + lists.size() + "[index]" + arg2);
				}
			}
		});
		loadList(0);
	}

	private List<I_List> lists = new ArrayList<>();
	private CourseAdapter adapter = null;
	private static String type = null;
	private int page = 0;

	private void loadList(int my_page) {
		FavouriteCoursesRequest request = new FavouriteCoursesRequest(this, API.personCenter.studyrecord);
		Query query = new Query();
		query.setPage(my_page);
		query.setLength(5);
		try {
			request.setParams(new UserManger(this).getUser().getUser_id());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if (my_page == 0) {
			page = my_page;
			lists.clear();
			if (adapter == null) {
				adapter = (CourseAdapter) new CourseAdapter().instance(this, lists);
				listView.setAdapter(adapter);
			}
		}
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					JSONArray array = json.getJSONArray("data");
					if (array.size() > 0) {
						for (int i = 0; i < array.size(); i++) {
							Course course = (Course) new Course().instance(array.getJSONObject(i));
							lists.add(course);
						}
						adapter.notifyDataSetChanged();
						page++;
					}
					// Call onRefreshComplete when the list has been refreshed.
					mPullRefreshListView.onRefreshComplete();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				// Call onRefreshComplete when the list has been refreshed.
				adapter.notifyDataSetChanged();
				mPullRefreshListView.onRefreshComplete();
				// if (page == 0) {
				// T.toast("数据为空");
				// }
			}
		});
		if (page == 0) {
			request.show();
		} else {
			request.start();
		}
	}
}
